/*
 This file 'login' is part of Firebird Integrated Solution 1.0

 Copyright (c) 2015 Lincong

 Contact:  
 Email: lincong1987@gmail.com

 QQ: 159257119

 See Usage at http://www.jplatformx.com/firebird

 Create date: 2015-03-17 03:09
 */

define(function (require, exports, module) {

	require("dialog");
	require("base64");

	var login = {
		url: {},
		message: {}
	};

	var replaceContextPath = function (path) {
		return $.trim(path).replace(/\{contextPath\}/g, firebird.api.get("contextPath").replace(/\/$/, ""));
	};

	login.url.login = "{contextPath}/security/user/login";
	login.url.show = "{contextPath}/security/user/show";
	login.url.captcha = "{contextPath}/captcha";
	login.url.success = "{contextPath}/fb-classic/main.jsp";
	login.message.logining = "登录中...";
	login.message.network_error = "网络异常";
	login.message.login = "登录";
	login.message.welcome = "欢迎回来!";
	login.message.login_success = "登录成功";
	login.message.login_error = "用户名或密码错误";

	$("#name, #password, #captcha").on("keydown keyup keypress focus blur", function (e) {
		if ($(this).val() == "") {
			$(this).next().show(0);
		} else {
			$(this).next().hide(0);
		}
	}).on("focus", function () {
		$(this).parent().addClass("focus");
	}).on("blur", function () {
		$(this).parent().removeClass("focus");
	});

	setTimeout(function () {
		$("#name, #password, #captcha").each(function () {
			if ($(this).val() == "") {
				$(this).next().show(0);
			} else {
				$(this).next().hide(0).val($(this).val());
			}
		});
	}, 0);

	$("#name").on("keydown", function (e) {
		if (e.keyCode && e.keyCode == firebird.keyCode.ENTER) {
			$("#password").focus();
		}
	});
	$("#password").on("keydown", function (e) {
		if (e.keyCode && e.keyCode == firebird.keyCode.ENTER) {
			if ($(".ui-login-form-nocaptcha")) {
				$("#captcha").focus();
			} else {
				doLogin();
			}
		}
	});
	$("#captcha").on("keydown", function (e) {
		if (e.keyCode && e.keyCode == firebird.keyCode.ENTER) {
			doLogin();
		}
	});

	$("#captcha_image").on("click", function (e) {
		$(this).attr({
			src: replaceContextPath(login.url.captcha) + "?_=" + new Date().getTime()
		});

		setTimeout(function () {
			$("#captcha").val("").focus();
		}, 0);
	});

	$(".ui-login-form-field-placeholder").on("click", function (e) {
		$(this).prev().focus();
	});

	$("#login_btn").on("click", doLogin);

	function doLogin() {

		var btn = $("#login_btn");
		btn.prop({disabled: true}).text(login.message.logining);

		$.ajax({
			type: "post",
			dataType: "json",
			url: replaceContextPath(login.url.login),
			data: login.getParams({
				name: $("#name").val(),
				password: $("#password").val(),
				captcha: $("#captcha").val()
			}),
			error: function () {
				alert(login.message.network_error);
				btn.prop({disabled: false}).text(login.message.login);
			},
			success: function (json) {
				if (json.success == true) {
					btn.text(login.message.login_success);
					location.href = replaceContextPath(login.url.success);
				} else {
					alert(json.errMsg || login.message.login_error);
					$("#captcha").val("");
					$("#captcha_image").attr({
						src: replaceContextPath(login.url.captcha) + "?_=" + new Date().getTime()
					});
					btn.prop({disabled: false}).text(login.message.login);
				}
			}

		});

	}

	login.doLogin = doLogin;

	login.getParams = function (params) {
		return params;
	};

	(function () {

		var tips = firebird.url.getParameter("tips");
		if (tips === "permission_error") {
			location.hash = "";
			$.dialog.tips("会话已失效，请重新登录。", 2000);
			return;
		}
		if (tips === "wait_u_back") {
			location.hash = "";
			$.dialog.tips("已退出。", 2000);
		}

		$.ajax({
			type: "post",
			dataType: "json",
			url: replaceContextPath(login.url.show),
			success: function (json) {
				if (json.success == true) {
					$.dialog.tips(login.message.welcome, 2000);
					setTimeout(function () {
						location.href = replaceContextPath(login.url.success);
					}, 2000);
				}
			}
		});
	})();

	setTimeout(function () {
		var $name = $("#name");
		$name.focus().val($name.val());
	}, 0);

	module.exports = login;
});